<?php
$this->headScript()->appendFile('js/jquery/datepicker/ui.datepicker.ext.js');
$this->headScript()->appendFile('/js/ecart/admin/discount/form.js');
?>
<script type="text/javascript">
    var Discount = {
        id : <?php echo empty($this->discount['id']) ? 0 : $this->discount['id']; ?>
    };

    var Attributes = <?php echo Zend_Json_Encoder::encode($this->attributes); ?>;
</script>

<div id="templates" style="display: none;">
    <?php echo $this->render('discount-index/condition/price.phtml'); ?>
    <?php echo $this->render('discount-index/condition/date.phtml'); ?>
    <?php echo $this->render('discount-index/condition/productId.phtml'); ?>
    <?php echo $this->render('discount-index/condition/attribute.phtml'); ?>
</div>

<?php
$this->setButtons(array(
    array(
        'title' => $this->translate('admin')->__('Back'),
        'onclick' => 'history.back()',
        'class' => 'alt-button'
    ),
    array(
        'title' => $this->translate('admin')->__('Save'),
        'onclick' => 'saveBackDiscount(); return false;',
        'class' => 'button'
    )
));

$this->setTabs(array(
    array('id' => 'general', 'title' => $this->translate('discount')->__('General')),
    array('id' => 'manufact', 'title' => $this->translate('catalog')->__('Manufacturer')),
    array('id' => 'categories', 'title' => $this->translate('catalog')->__('Categories')),
    array('id' => 'product-conditions', 'title' => $this->translate('discount')->__('Additional Conditions'))
), true);
?>

<form id="form-discount" action="">

    <div id="tab-general-block" class="form-wrapper">
        <p>
            <label><?php echo $this->translate('discount')->__('Discount Name')?></label>
            <div class="table-error-arrow">
                <p class="table-error-text">
                    <?php echo $this->translate('discount')->__('You can not use those symbols')?>: / @ ! # $ % ^ &gt; * ( )
                </p>
            </div>
            <?php echo $this->formText(
                'discountName',
                isset($this->discount['name']) ? $this->discount['name'] : null,
                array('size' => '50')
            );?>
        </p>
        <p>
            <label><?php echo $this->translate('discount')->__('Status')?></label>
            <?php echo $this->formSelect(
                'discountIsActive',
                isset($this->discount['is_active']) ? $this->discount['is_active'] : null,
                null,
                array('1' => 'Enabled', '0' => 'Disabled'
            )); ?>
        </p>
        <?php if (isset($this->discount['special']))
            echo $this->formHidden('discountSpecial', $this->discount['special']);
         ?>
        <p>
            <label><?php echo $this->translate('core')->__('Sites')?></label>
            <?php echo $this->formSelect(
                'discountSites[]',
                isset($this->discount['site_ids']) ? $this->discount['site_ids'] : null,
                array('size' => '5'),
                $this->sites
            );?>
        </p>
        <p>
            <label><?php echo $this->translate('discount')->__('Customer Groups')?></label>
            <?php echo $this->formSelect(
                'discountCustomerGroups[]',
                isset($this->discount['customer_group_ids']) ? $this->discount['customer_group_ids'] : null,
                array('size' => '5'),
                $this->customerGroups
            );?>
        </p>
        <p>
            <label><?php echo $this->translate('discount')->__('From Date')?></label>
            <?php echo $this->formText(
                'discountFromDate',
                isset($this->discount['from_date']) ? $this->discount['from_date'] : null,
                array('class' => 'date-picker')
            );?>
        </p>
        <p>
            <label><?php echo $this->translate('discount')->__('To Date')?></label>
            <?php echo $this->formText(
                'discountToDate',
                isset($this->discount['to_date']) ? $this->discount['to_date'] : null,
                array('class' => 'date-picker')
            );?>
        </p>
        <p>
            <label><?php echo $this->translate('discount')->__('Discount Priority')?></label>
            <?php echo $this->formText(
                'discountPriority',
                isset($this->discount['priority']) ? $this->discount['priority'] : null,
                array('size' => '50')
            );?>
        </p>
        <p>
            <label><?php echo $this->translate('discount')->__('Is Combined')?></label>
            <?php echo $this->formSelect(
                'discountIsCombined',
                isset($this->discount['is_combined']) ? $this->discount['is_combined'] : null,
                null,
                array('1' => 'Enabled', '0' => 'Disabled'
            )); ?>
        </p>
        <p>
            <label><?php echo $this->translate('discount')->__('Discount type')?></label>
            <?php echo $this->formSelect(
                'discountOperator',
                isset($this->discount['type']) ? $this->discount['type'] : null,
                null,
                array(
                'to' => 'Fixed Price',
                'by' => 'Fixed Amount',
                'percent' => 'Percent'
                )
            ); ?>
        </p>
        <p>
            <label><?php echo $this->translate('discount')->__('Update value')?></label>
            <?php echo $this->formText(
                'discountAmount',
                isset($this->discount['amount']) ? $this->discount['amount'] : null
            ); ?>
        </p>

    </div>
    <div id="tab-manufact-block" class="form-wrapper">
    <?php echo $this->formSelect(
        'condition[manufacture][]',
        isset($this->discount['manufacture']) ? $this->discount['manufacture'] : null,
        array('size' => '5'),
        $this->manufactures
    );
    ?>
    </div>
    <div id="tab-categories-block" class="form-wrapper">
    <?php foreach ($this->categoryTrees as $siteId => $items) {
        echo '<div class="toggle-header green-header">' . $this->sites[$siteId] . '</div>';
        echo '<div class="categories">';
        foreach ($items as $item) {
            echo '<div class="tree-cat" style="margin-left: '. ($item['lvl'] * 17) .'px">'
                . '<input name="condition[category][]" type="checkbox" '
                . (isset($this->discount['category']) &&
                in_array($item['id'], $this->discount['category']) ?
                ' checked="1"' : '')
                . 'value="' . $item['id'] . '" /> '
                . $item['name'] . '</div>';
        }
        echo '</div>';
    } ?>
    </div>
    <div id="tab-product-conditions-block">
        <div id="condition-list"><?php
            if (isset($this->discount['conditions']['date']))
                foreach ($this->discount['conditions']['date']['e-type'] as $i => $etype) {
                    $this->dateEqualType = $etype;
                    $this->dateValue = $this->discount['conditions']['date']['value'][$i];
                    echo $this->render('discount-index/condition/date.phtml');
                }

            if (isset($this->discount['conditions']['price']))
                foreach ($this->discount['conditions']['price']['e-type'] as $i => $etype) {
                    $this->priceEqualType = $etype;
                    $this->priceValue = $this->discount['conditions']['price']['value'][$i];
                    echo $this->render('discount-index/condition/price.phtml');
                }

            if (isset($this->discount['productId']))
                foreach ($this->discount['productId'] as $productId) {
                    $this->prodIdValue = $productId;
                    echo $this->render('discount-index/condition/productId.phtml');
                }

            if (isset($this->discount['attributes']))
                foreach ($this->discount['attributes'] as $attribute) {
                    $this->attribute = $attribute;
                    echo $this->render('discount-index/condition/attribute.phtml');
                }
            ?>
        </div>
        <div>
            <select onchange="addCondition(this);return false;">
                <option value="empty"><?php echo $this->translate('discount')->__("Add")?></option>
                <option value="attribute"><?php echo $this->translate('discount')->__("Attribute")?></option>
                <option value="price"><?php echo $this->translate('discount')->__("Product price")?></option>
                <option value="date"><?php echo $this->translate('discount')->__("Product date")?></option>
                <option value="product"><?php echo $this->translate('discount')->__("Product (id)")?></option>
            </select>
        </div>
    </div>
</form>